package com.xqboss.apps.mapper.hama;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xqboss.apps.domain.hama.HamaHelp;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xqboss.apps.vo.hama.HamaHelpAdminVO;
import com.xqboss.apps.vo.hama.HamaHelpMemberVO;
import com.xqboss.apps.vo.hama.HamaHelpNftVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 助力灵池 Mapper 接口
 * </p>
 *
 * @author 木鱼
 * @since 2024-02-28
 */
@Mapper
@Repository
public interface HamaHelpMapper extends BaseMapper<HamaHelp> {

    @Select("SELECT\n" +
            "\t( SELECT count(*) s FROM hama_help WHERE parent_user_id = #{userId}  AND basic_speed > 0 AND del_flag = 0 ) + (\n" +
            "\tSELECT\n" +
            "\t\tcount(*) s \n" +
            "\tFROM\n" +
            "\t\thama_help \n" +
            "\tWHERE\n" +
            "\t\tparent_user_id IN ( SELECT child_user_id FROM hama_help WHERE parent_user_id = #{userId}  AND is_people != 4 AND del_flag = 0 ) \n" +
            "\t\tAND is_people != 4 \n" +
            "\tAND egg_laying_num > 0 \n" +
            "\tAND del_flag = 0)")
    int countManNumber(@Param("userId") Long userId);

    /*直推助力*/
    @Select("select sum(basic_speed) from hama_help where parent_user_id=#{userId} and basic_speed>0 and del_flag = 0")
    BigDecimal sumManSpeed(@Param("userId") Long userId);

    /*间推助力*间推系数*/
    @Select("SELECT\n" +
            "\tSUM(basic_speed) \n" +
            "FROM\n" +
            "\thama_help \n" +
            "WHERE\n" +
            "\tparent_user_id IN ( SELECT child_user_id FROM hama_help WHERE parent_user_id = #{userId} AND is_people != 4 AND del_flag = 0 ) \n" +
            "\tAND is_people != 4 \n" +
            "\tand egg_laying_num>0\n" +
            "\tAND del_flag = 0")
    BigDecimal sumManSpeedSon(@Param("userId") Long userId);

    @Select("select sum(egg_laying_num) from hama_help where parent_user_id=#{userId} and egg_laying_num>0 and del_flag = 0")
    BigDecimal sumManHelpDirect(@Param("userId") Long userId);

    @Select("select count(*) from hama_help where parent_user_id=#{userId} and is_people !=4 and del_flag = 0")
    int sumManDirect(@Param("userId") Long userId);

    @Select("SELECT\n" +
            "\tSUM(egg_laying_num_indirect) \n" +
            "FROM\n" +
            "\thama_help \n" +
            "WHERE\n" +
            "\tparent_user_id IN ( SELECT child_user_id FROM hama_help WHERE parent_user_id = #{userId} AND is_people != 4 AND del_flag = 0 ) \n" +
            "\tAND is_people != 4 \n" +
            "\tand egg_laying_num>0\n" +
            "\tAND del_flag = 0")
    BigDecimal sumManHelpIndirect(@Param("userId") Long userId);

    @Select("SELECT\n" +
            "\tCOUNT(*) \n" +
            "FROM\n" +
            "\thama_help \n" +
            "WHERE\n" +
            "\tparent_user_id IN ( SELECT child_user_id FROM hama_help WHERE parent_user_id = #{userId}  AND is_people != 4 AND del_flag = 0 ) \n" +
            "\tAND is_people != 4 \n" +
            "\tAND del_flag = 0")
    int sumManInDirect(@Param("userId") Long userId);

    IPage<HamaHelpAdminVO> selectTeamAdmins(Page<HamaHelpAdminVO> page,@Param("search") String search);

    List<HamaHelpNftVO> selectHelpNftvos(@Param("memberUserId") Long memberUserId);

    IPage<HamaHelp> selectTeamMembers(Page<HamaHelp> hamaHelpPage,@Param("userId") Long userId);
}
